﻿$(function (e) {
    var reentrance = false;
    $('[data-rel=save]').unbind("click").click(onSaveClick);

    function onSaveClick() {
        if (reentrance) return false;
        reentrance = true;
        var data = {};
        var textBox = $('[personId]');
        data.personId = textBox.attr('personId');
        data.personName = textBox.attr('value');
        $.ajax({
            url: '/Home/SavePerson',
            type: 'POST',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: JSON.stringify(data),
        }).done(function (res) {
            reentrance = false;
            $('#errorHolder').hide();
            if (res.error) {
                $('#errorHolder').show();
                $('#errorHolder').html(res.error);
            } else {
                window.location.href = res.redirectToUrl;
            }
        });

        return false; // supposed to fix double events; calls stopPropagation and preventDefault
    };

});

